#include <stdlib.h>
#include <stdio.h>
#include <nds.h>

// common headers
#include "common.h"

// local headers
#include "filelist.h"

#include "main.h"

#define _VERSION_INT MAKE_VERSION(0, 1, 0, STATE_RC, 1)
char _VERSION_STR[VERSION_STR_MAX_LEN+1];

int init() {
	std->sprintf(_VERSION_STR, VERSION_FORMAT, VERSION_ARGS(_VERSION_INT));	// make version str

	return 0;
}

u32 query(u32 query) {
	switch(query) {
	case QUERY_NAME:		// plugin name
		return (u32)"filebrowser";
	case QUERY_VERSION_INT:	// plugin integer version
		return (u32)_VERSION_INT;
	case QUERY_VERSION_STR:	// plugin string version
		return (u32)_VERSION_STR;
	case QUERY_AUTHOR:		// plugin author
		return (u32)"Ludo6431";
	case QUERY_FILEEXT:		// plugin file extension handled
		return (u32)NULL;
	default:
		return 0;
	}
}

int main(int argc, char *argv[]) {
	bool quit=0;
	char _path[512];
	char *path;
	u32 i;
	char *selectpath=NULL;
	std->chdir("/");
	std->iprintf("\x1b[32;1m");	// green
	nds->keysSetRepeat(35, 8);

	LIFO *pathlist=lf->new();

// main loop
	while(1) {
		LLIST *filelist=NULL;

		std->getcwd(_path, 512);
		if(std->strchr(_path, ':')) path=std->strchr(_path, ':')+1;
		else path=_path;

		filelist=getFileList(selectpath);
		if(!filelist) {
			std->iprintf("!getFileList()");
			return 1;
		}
		if(selectpath) x->xfree(selectpath);	//|	free and
		selectpath=NULL;						//| set the pointer to NULL

		u8 update=1;
		while(1) {
			nds->scanKeys();
			u32 keysD=nds->keysDown();
			u32 keysDR=nds->keysDownRepeat();

			if(update) {
				update=0;
				std->iprintf("\x1b[2J");	// clear screen
				printFileList(filelist, path);	// print the list
			}

			if(filelist->next) {
				if(keysDR & KEY_DOWN) filelist=filelist->next;
				if(keysDR & KEY_RIGHT) for(i=0; i<5 && filelist->next; i++,filelist=filelist->next);
				update=1;
			}
			if(filelist->prev) {
				if(keysDR & KEY_UP) filelist=filelist->prev;
				if(keysDR & KEY_LEFT) for(i=0; i<5 && filelist->prev; i++,filelist=filelist->prev);
				update=1;
			}
			if(keysD & KEY_A) {	// don't allow repeat for this key
				if(SF(filelist)->type) {	// a file
					sPlugin *plug=api->getPluginByExt(SF(filelist)->name);
					if(plug) {
						char *argv[1] = { SF(filelist)->name };
						api->runPlugin(plug, 1, argv);
						update=1;
					}
				}
				else {	// a directory
					if(!std->strcmp(SF(filelist)->name, ".."))
						selectpath=(char *)lf->pop(pathlist);
					else
						lf->push(pathlist, x->xstrdup(SF(filelist)->name));

					std->strcat(path, SF(filelist)->name);
					std->chdir(path);
					break;
				}
			}
			if(keysD & KEY_START) {	// quit
				quit=1;
				break;
			}
			if((keysDR & KEY_B) && std->strcmp(path, "/")) {
				selectpath=(char *)lf->pop(pathlist);

				std->chdir("..");
				break;
			}

			nds->swiWaitForVBlank();
		}

		ll->free_all_handler(filelist, (datahandler)x->xfree);	// free the filelist

		if(quit) break;
	}

	lf->free(pathlist);

	std->iprintf("\x1b[2J");	// clear screen

	return 0;
}

void deinit() {
}

